#include "Marker.h"
#include "ui_MainWindow.h"
#include "MainWindow.h"
#include <QtGui>

Marker::Marker()
{

    myOutlineColor = Qt::yellow;
    myBackgroundColor = Qt::transparent;
    myShape =Circle;
    mySize=25;

    setFlags(ItemIsMovable | ItemIsSelectable);
}


QRectF Marker::outlineRect() const
{
    const int Padding = 1;

    QRectF rect(0,0,mySize,mySize);
    rect.adjust(-Padding, -Padding, +Padding, +Padding);
    rect.translate(-rect.center());
    return rect;
}

QRectF Marker::boundingRect() const
{
    const int Margin = 1;
    return outlineRect().adjusted(-Margin, -Margin, +Margin, +Margin);
}

QPainterPath Marker::shape() const
{
    QRectF rect = outlineRect();

    QPainterPath path;

    path.addRoundRect(rect, roundness(rect.width()),roundness(rect.height()));

    return path;
}

void Marker::paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget * /* widget */)
{
    QPen pen(myOutlineColor);
    pen.setWidth(2);

    if (option->state &QStyle::State_Selected)
    {
        pen.setStyle(Qt::DashDotLine);
        pen.setWidth(3);
        pen.setColor(Qt::cyan);
    }

    painter->setPen(pen);
    painter->setBrush(myBackgroundColor);

    QRectF rect = outlineRect();
    painter->drawRoundRect(rect, roundness(rect.width()),roundness(rect.height()));

    update();
}

int Marker::roundness(double size) const
{
    int Diameter=0;

    switch (myShape)
    {
    case Square:
        Diameter=12;
        break;
    case Circle:
        Diameter=99;
        break;
    case Triangle:
        Diameter=12;
        break;
    }


    return 100 * Diameter / int(size);
}


QVariant Marker::itemChange(GraphicsItemChange change,
                          const QVariant &value)
{
    //if (change == ItemPositionHasChanged) {
        //foreach (Link *link, myLinks)
            //link->trackNodes();
    //}
    return QGraphicsItem::itemChange(change, value);
}

void Marker::setOutlineColor(const QColor &color)
{
    this->myOutlineColor = color;
    update();
}

void Marker::setShape(Shape shape)
{
    this->myShape = shape;
    update();
}

void Marker::setSize(short size)
{
    this->mySize = size;
    update();
}





